﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YZQ_DotNetArx.CAD;

namespace YZQ_DotNetArx.Test
{
    public static class 面域
    {
        [CommandMethod("AddRegion")]
        public static void AddRegion()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //创建一个三角形
                Polyline triangle = new Polyline();
                triangle.CreatePolygon(new Point2d(550, 20), 3, 30);
                //根据三角形创建面域
                List<Region> regions = RegionTools.CreateRegion(triangle);
                if (regions.Count == 0) return; //如果创建面域未成功，则返回
                Env.AddEntityToModelSpace(regions[0]); //将创建的面域添加到数据库中
                //获取面域的质量特性
                getAreaProp(regions[0]);
                trans.Commit();
            }
        }
        private static void getAreaProp(Region region)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\n------------面域-----------");
            ed.WriteMessage("\n面积：{0}", region.Area);
            ed.WriteMessage("\n周长：{0}", region.Perimeter);
            ed.WriteMessage("\n边界框上限：{0}", region.GetExtentsHigh());
            ed.WriteMessage("\n边界框下限：{0}", region.GetExtentsLow());
            ed.WriteMessage("\n质心：{0}", region.GetCentroid());
            ed.WriteMessage("\n惯性矩为：{0}", region.GetProdInertia());
        }
    }
}
